package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.CarPageModel;
import com.southwind.common.ResultObj;
import com.southwind.common.WorkerPageModel;
import com.southwind.entity.Car;
import com.southwind.entity.User;
import com.southwind.entity.Worker;
import com.southwind.mapper.CarMapper;
import com.southwind.service.CarService;
import com.southwind.service.UserService;
import com.southwind.service.WorkerService;
import com.southwind.vo.CarVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/worker")
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    @GetMapping("/list")
    public Map list(WorkerPageModel workerPageModel){
        Page<Worker> page = new Page<>(workerPageModel.getPage(), workerPageModel.getLimit());
        QueryWrapper<Worker> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(workerPageModel.getUsername()), "username", workerPageModel.getUsername());
        Page<Worker> resultPage = this.workerService.page(page, queryWrapper);
        Map map = new HashMap();
        map.put("count", resultPage.getTotal());
        map.put("data", resultPage.getRecords());
        return map;
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody Worker worker){
        boolean save = this.workerService.save(worker);
        if(save) return ResultObj.WORKER_ADD_SUCCESS;
        return ResultObj.WORKER_ADD_ERROR;
    }

    @PostMapping("/update")
    public ResultObj update(@RequestBody Worker worker){
        boolean updateById = this.workerService.updateById(worker);
        if(updateById) return ResultObj.WORKER_UPDATE_SUCCESS;
        return ResultObj.WORKER_UPDATE_ERROR;
    }

    @DeleteMapping("/delete/{id}")
    public ResultObj delete(@PathVariable("id") Integer id){
        boolean removeById = this.workerService.removeById(id);
        if(removeById) return ResultObj.WORKER_DELETE_SUCCESS;
        return ResultObj.WORKER_DELETE_ERROR;
    }

}

